package com.xudu.tourbackend.controller.Impl;

import com.xudu.tourbackend.controller.TestController;
import com.xudu.tourbackend.entity.ScenicArea;
import com.xudu.tourbackend.entity.ScenicSpot;
import com.xudu.tourbackend.entity.request.TestArea;
import com.xudu.tourbackend.mapper.ScenicAreaMapper;
import com.xudu.tourbackend.mapper.ScenicSpotMapper;
import com.xudu.tourbackend.utils.QiniuUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @className: TestControllerImpl
 * @description: TODO
 * @author: xudu
 * @create: 2024-06-20
 */
@RestController
public class TestControllerImpl implements TestController {

    @Resource
    private QiniuUtil qiniuUtil;

    @Resource
    private ScenicAreaMapper scenicAreaMapper;

    @Resource
    private ScenicSpotMapper scenicSpotMapper;


    @Override
    public String upload(@RequestParam("file") MultipartFile file) {
        return qiniuUtil.upload(file);
    }

    @Override
    public void delete(String fileAdress) {
        qiniuUtil.deleteFile(fileAdress);
    }

    @Override
    public ScenicArea addScenicArea(@RequestPart @Validated TestArea testArea) {
        String name = testArea.getName();
        double lat = testArea.getLat();
        double lng = testArea.getLng();
        ArrayList<ScenicSpot> list = new ArrayList<>();
        for (int spot : testArea.getSpots()) {
            ScenicSpot scenicSpot = scenicSpotMapper.selectById(spot);
            list.add(scenicSpot);
        }


        MultipartFile file1 = testArea.getFile1();
        MultipartFile file2 = testArea.getFile2();
        ScenicArea scenicArea = new ScenicArea();
        //scenicArea.setSpots(list);
        scenicArea.setScenicAreaName(name);
        scenicArea.setScenicAreaLatitude(lat);
        scenicArea.setScenicAreaLongitude(lng);
        scenicArea.setScenicAreaImage(qiniuUtil.upload(file1));
        scenicArea.setScenicAreaVoice(qiniuUtil.upload(file2));
        scenicAreaMapper.insert(scenicArea);
        return scenicArea;
    }
}
